package net.technics;

import java.util.ArrayList;
import java.util.List;

import net.hibernate.HibernateUtil;
import net.models.Application;
import net.models.Praticiens;
import net.models.TypesPraticiens;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.hibernate.Transaction;

public class PraticienSupprimer {
	private DataBindingContext		m_bindingContext;

	protected Shell					shlBackOfficeManagement;
	private Text					textNom;
	private Text					textPrenom;
	private Text					textAdresse;
	private Text					textCP;
	private Text					textVille;
	private Text					txtEditerUnPraticien;
	private List<Praticiens>		praticiens		= new ArrayList<Praticiens>();
	private Combo					cmbPraticien;
	private ComboViewer				cmbViewerPraticien;
	private List<TypesPraticiens>	typesPraticiens	= new ArrayList<TypesPraticiens>();
	private Combo					cmbTypePraticien;
	private ComboViewer				cmbViewerTypePra;
	private Praticiens				praticien		= new Praticiens();

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					PraticienSupprimer window = new PraticienSupprimer();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		typesPraticiens = Application.getTypesPraticiens();
		praticiens = Application.getPraticiens();
		Display display = Display.getDefault();
		createContents();
		shlBackOfficeManagement.open();
		shlBackOfficeManagement.layout();
		while (!shlBackOfficeManagement.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlBackOfficeManagement = new Shell();
		shlBackOfficeManagement.setText("Back Office Management");
		shlBackOfficeManagement.setSize(306, 384);

		Composite composite_1 = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite_1.setBounds(10, 108, 278, 179);

		CLabel lblNom = new CLabel(composite_1, SWT.NONE);
		lblNom.setBounds(10, 10, 61, 21);
		lblNom.setText("Nom");

		CLabel lblPrenom = new CLabel(composite_1, SWT.NONE);
		lblPrenom.setText("Prénom");
		lblPrenom.setBounds(10, 37, 61, 21);

		CLabel lblAdresse = new CLabel(composite_1, SWT.NONE);
		lblAdresse.setText("Adresse");
		lblAdresse.setBounds(10, 64, 61, 21);

		CLabel lblCodePostal = new CLabel(composite_1, SWT.NONE);
		lblCodePostal.setText("Code postal");
		lblCodePostal.setBounds(10, 91, 94, 21);

		CLabel lblVille = new CLabel(composite_1, SWT.NONE);
		lblVille.setText("Ville");
		lblVille.setBounds(10, 118, 61, 21);

		CLabel lblGroupe = new CLabel(composite_1, SWT.NONE);
		lblGroupe.setText("Type de praticien");
		lblGroupe.setBounds(10, 145, 127, 21);

		textNom = new Text(composite_1, SWT.BORDER);
		textNom.setEnabled(false);
		textNom.setBounds(144, 10, 124, 21);

		textPrenom = new Text(composite_1, SWT.BORDER);
		textPrenom.setEnabled(false);
		textPrenom.setBounds(144, 37, 124, 21);

		textAdresse = new Text(composite_1, SWT.BORDER);
		textAdresse.setEnabled(false);
		textAdresse.setBounds(144, 64, 124, 21);

		textCP = new Text(composite_1, SWT.BORDER);
		textCP.setEnabled(false);
		textCP.setBounds(144, 91, 124, 21);

		textVille = new Text(composite_1, SWT.BORDER);
		textVille.setEnabled(false);
		textVille.setBounds(144, 118, 124, 21);

		cmbViewerTypePra = new ComboViewer(composite_1, SWT.NONE);
		cmbTypePraticien = cmbViewerTypePra.getCombo();
		cmbTypePraticien.setBounds(143, 145, 125, 23);

		Composite composite_2 = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite_2.setBounds(81, 293, 207, 48);

		Button button = new Button(composite_2, SWT.NONE);
		button.setText("Annuler");
		button.setBounds(106, 10, 90, 30);

		Button btnSupprimer = new Button(composite_2, SWT.NONE);
		btnSupprimer.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				IStructuredSelection sel = (IStructuredSelection) (cmbViewerPraticien.getSelection());
				praticien = (Praticiens) (sel.iterator().next());
				org.hibernate.Session session = (org.hibernate.Session) HibernateUtil.getSession();
				Transaction trans = session.beginTransaction();
				// Query query =
				// session.createQuery("from Praticiens p where p.id = :pId");
				// query.setParameter("pId", praticien.getId());
				Praticiens prat = new Praticiens(praticien.getId());
				Application.getPraticiens().remove(prat);
				cmbViewerPraticien.remove(praticien);
				session.delete(praticien);
				trans.commit();
				session.close();
				System.out.println(praticien.getId());
			}
		});
		btnSupprimer.setText("Valider");
		btnSupprimer.setBounds(10, 10, 90, 30);

		Composite composite_3 = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite_3.setBounds(10, 54, 278, 48);

		CLabel lblPraticien = new CLabel(composite_3, SWT.NONE);
		lblPraticien.setText("Praticien");
		lblPraticien.setBounds(10, 12, 75, 21);

		cmbViewerPraticien = new ComboViewer(composite_3, SWT.NONE);
		cmbPraticien = cmbViewerPraticien.getCombo();

		cmbPraticien.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				IStructuredSelection sel = (IStructuredSelection) (cmbViewerPraticien.getSelection());
				praticien = (Praticiens) (sel.iterator().next());
				Praticiens prat = praticien;

				textNom.setEnabled(true);
				textNom.setText(praticien.getName());
				textPrenom.setEnabled(true);
				textPrenom.setText(praticien.getPrenom());
				textAdresse.setEnabled(true);
				textAdresse.setText(praticien.getAdresse());
				textCP.setEnabled(true);
				textCP.setText(praticien.getCp());
				textVille.setEnabled(true);
				textVille.setText(praticien.getVille());
				cmbViewerTypePra.setSelection(new StructuredSelection(praticien.getTypesPraticiens()));
			}

		});
		cmbPraticien.setBounds(143, 12, 125, 23);

		Composite composite = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_BLUE));
		composite.setBounds(0, 0, 288, 48);

		txtEditerUnPraticien = new Text(composite, SWT.NONE);
		txtEditerUnPraticien.setText("Supprimer un praticien");
		txtEditerUnPraticien.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		txtEditerUnPraticien.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.BOLD));
		txtEditerUnPraticien.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_BLUE));
		txtEditerUnPraticien.setBounds(10, 10, 205, 21);
		//
		m_bindingContext = initDataBindings();
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap observeMap = BeansObservables.observeMap(listContentProvider.getKnownElements(), TypesPraticiens.class, "libelle");
		cmbViewerTypePra.setLabelProvider(new ObservableMapLabelProvider(observeMap));
		cmbViewerTypePra.setContentProvider(listContentProvider);
		//
		IObservableList selfList = Properties.selfList(TypesPraticiens.class).observe(typesPraticiens);
		cmbViewerTypePra.setInput(selfList);
		//
		ObservableListContentProvider listContentProvider_1 = new ObservableListContentProvider();
		IObservableMap[] observeMaps = BeansObservables.observeMaps(listContentProvider_1.getKnownElements(), Praticiens.class, new String[] { "name", "prenom" });
		cmbViewerPraticien.setLabelProvider(new ObservableMapLabelProvider(observeMaps));
		cmbViewerPraticien.setContentProvider(listContentProvider_1);
		//
		IObservableList selfList_1 = Properties.selfList(Praticiens.class).observe(praticiens);
		cmbViewerPraticien.setInput(selfList_1);
		//
		return bindingContext;
	}
}
